<template>
  <div>
    <input type="file" :accept="accept"
           ref="input"
           @change="inputChange" />
  </div>
</template>

<script>
  let defaultOption = {
    // upload
    accept:"*",
    // download
    fileName:"download"
  }
  let onBefore = function(){
    // 前置校验
    return true;
  }

  export default {
    name: "DialogEdit",
    data() {
      return {
        ...defaultOption
      }
    },
    methods: {
      onUpload(options){
        Object.assign(this,{onBefore,...defaultOption,...options});
        this.$refs.input.click();
      },
      inputChange(e){
        let file = e.target.files[0]; // 仅支持单个文件
        if(this.onBefore(file)){
          this.upload({file});
          e.target.value = "";
        }
      },
      upload({file}){
        // api
        this.api("base.upload",file).then((res)=>{
          if(this.onSuccess){
            this.onSuccess(res);
          }
        })
      }
    }
  }

</script>

<style>

</style>
